using System;
using CatLib.Container;
using GameFramework.Core;
using GameFramework.Event;
using GameFramework.Utility;

namespace GameFramework.Game
{
    /// <summary>
    /// 玩法系统接口
    /// </summary>
    public interface IGameSystem
    {
        /// <summary>
        /// 游戏开始
        /// </summary>
        void OnGameStart();

        /// <summary>
        /// 游戏停止
        /// </summary>
        void OnGameStop();
    }

    /// <summary>
    /// 玩法系统接口
    /// </summary>
    public abstract class AGameSystem : IGameSystem, IInitialize, IDisposable
    {
        [Inject]
        public IEventService EventService { get; set; }
        
        protected DisposableGroup DisposableGroup => m_DisposableGroup ??= new DisposableGroup();
        private DisposableGroup m_DisposableGroup;

        public void Initialize()
        {
            OnInitialize();
        }

        public void Dispose()
        {
            OnDispose();
            m_DisposableGroup?.Dispose();
            m_DisposableGroup = null;
        }

        protected void Subscribe<T>(EventHandler handler) where T : EventArgs
        {
            DisposableGroup.Add(EventService.SubscribeAsDisposable<T>(handler));
        }

        protected void Subscribe(string eventName, EventHandler handler)
        {
            DisposableGroup.Add(EventService.SubscribeAsDisposable(eventName, handler));
        }

        protected abstract void OnInitialize();
        protected abstract void OnDispose();
        public abstract void OnGameStart();
        public abstract void OnGameStop();
    }
}